package com.bsoft.exchange.controller.basicArchive;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bsoft.exchange.param.RegisterParam;
import com.bsoft.exchange.service.AppointmentService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bsoft.exchange.domain.PatientDomain;
import com.bsoft.exchange.pojo.PatientCodePojo;
import com.bsoft.exchange.pojo.RegisterPojo;
import com.bsoft.exchange.response.ResponseEntity;
import com.bsoft.exchange.service.PatientService;
import com.bsoft.exchange.service.RegisterService;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.DateUtil;
import com.bsoft.exchange.vo.CardVo;
import com.bsoft.exchange.vo.PatientCardVo;
import com.bsoft.exchange.vo.PatientVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


//import com.bsoft.exchange.domain.Test;
//import com.bsoft.exchange.service.TestService;

@Api("swagger测试控制器")
@RestController
@RequestMapping(value = "register") 
public class RegisterController {
	
	@Autowired
    Mapper dozerMapper;
	
	@Autowired
	RegisterService registerService;
	
	@Autowired
	PatientService patientService;

	@Autowired
	AppointmentService appointmentService;
	

//	@PostMapping("/getPhoneAndCards")
	@ApiOperation(value = "获取医院预留手机号和业务卡", notes = "入参：hospitalCode：机构id ； patientIdentityCardType ：证件类型 ；patientIdentityCardNumber：证件号码；"
			+ "patientName：患者姓名；patientSex：患者性别 ")
	@ResponseBody
	@RequestMapping(value = "/getPhoneAndCards",method = RequestMethod.POST,produces = "application/json")
	public ResponseEntity<List<PatientVo>> getPhoneAndCards( @RequestBody RegisterPojo registerPojo	) {
		
		System.out.println(registerPojo);

		ResponseEntity<List<PatientVo>> entry = new ResponseEntity();
		
		Map<String, Object> ret = new HashMap<String, Object>();

		ret = patientService.getPhoneAndCards( registerPojo.getOrganizationId(), 
				registerPojo.getCardType(),
				registerPojo.getCardNumber(),
				registerPojo.getPatientMedicalCardType(),
				registerPojo.getPatientMedicalCardNumber(),
				registerPojo.getPatientName(),
				registerPojo.getPatientSex(),
				registerPojo.getPatientBirthday(),
				registerPojo.getPatientMobile(),
				registerPojo.getProvinceCode(),
				registerPojo.getCityCode(),
				registerPojo.getAreaCode(),
				registerPojo.getPatientAddress(),registerPojo.getOperationType()
			);
		
		List<PatientDomain> patientList = (List<PatientDomain>) ret.get("patientList");
		String ywlx = (String) ret.get("ywlx");
		List<PatientVo> patientVoList = new ArrayList();
		//实体bean转换
		if(patientList!=null && patientList.size() > 0){

			String phone = "";
			String jzkh;
			String ybkh;
			String zyhm;
			String xzmc;
			Long brid;
			Integer patientMedicalCardType  = registerPojo.getPatientMedicalCardType();
			String patientMedicalCardNumber = registerPojo.getPatientMedicalCardNumber();
			for(PatientDomain patient : patientList){
				
				PatientVo patientVo = new PatientVo();
				
				phone = patient.getLxdh();
				List<CardVo> cardVoList = new ArrayList();
				//改为门诊号码
//				jzkh = patient.getJzkh();
				jzkh = patient.getMzhm();
				
				ybkh = patient.getYbkh();
				brid = patient.getBrid();
				xzmc = patient.getXzmc();
				zyhm = patient.getZyhm();
				if("卡查询".equals(ywlx) && patientMedicalCardType!=null && patientMedicalCardNumber !=null && !"".equals(patientMedicalCardNumber)){
					if(jzkh != null && !"".equals(jzkh) && jzkh.equals(patientMedicalCardNumber)){
						CardVo card = new CardVo();
						card.setPatientMedicalCardType(2);
						card.setPatientMedicalCardNumber(jzkh);
						card.setPatientId(brid.toString());
						card.setPatientNature(xzmc);
						cardVoList.add(card);
					}
					
					if(ybkh != null && !"".equals(ybkh)&& ybkh.equals(patientMedicalCardNumber)){
						CardVo ybkhCard = new CardVo();
						ybkhCard.setPatientMedicalCardType(3);
						ybkhCard.setPatientMedicalCardNumber(ybkh);
						ybkhCard.setPatientId(brid.toString());
						ybkhCard.setPatientNature(xzmc);
						cardVoList.add(ybkhCard);
					}
					
					if(zyhm != null && !"".equals(zyhm)&& zyhm.equals(patientMedicalCardNumber)){
						CardVo zyhmCard = new CardVo();
						zyhmCard.setPatientMedicalCardType(5);
						zyhmCard.setPatientMedicalCardNumber(zyhm);
						zyhmCard.setPatientId(brid.toString());
						zyhmCard.setPatientNature(xzmc);
						cardVoList.add(zyhmCard);
					}
					
				}else if("电话号码查询".equals(ywlx)){
					if(jzkh != null && !"".equals(jzkh) ){
						CardVo card = new CardVo();
						card.setPatientMedicalCardType(2);
						card.setPatientMedicalCardNumber(jzkh);
						card.setPatientId(brid.toString());
						card.setPatientNature(xzmc);
						cardVoList.add(card);
					}
					
					if(ybkh != null && !"".equals(ybkh)){
						CardVo ybkhCard = new CardVo();
						ybkhCard.setPatientMedicalCardType(3);
						ybkhCard.setPatientMedicalCardNumber(ybkh);
						ybkhCard.setPatientId(brid.toString());
						ybkhCard.setPatientNature(xzmc);
						cardVoList.add(ybkhCard);
					}
					
					if(zyhm != null && !"".equals(zyhm)){
						CardVo zyhmCard = new CardVo();
						zyhmCard.setPatientMedicalCardType(5);
						zyhmCard.setPatientMedicalCardNumber(zyhm);
						zyhmCard.setPatientId(brid.toString());
						zyhmCard.setPatientNature(xzmc);
						cardVoList.add(zyhmCard);
					}
				}
				patientVo.setPatientMobile(phone);
				if("建档".equals(ywlx)){
					CardVo jdCard = new CardVo();
					
					//加上门诊号码
					jdCard.setPatientMedicalCardType(2);
					jdCard.setPatientMedicalCardNumber(jzkh);
					
					jdCard.setPatientId(brid.toString());
					jdCard.setPatientNature(xzmc);
					cardVoList.add(jdCard);	
				}
				patientVo.setListMedicalCard(cardVoList);
				patientVoList.add(patientVo);
//				cardVoList.clear();
			}
		}else{
			entry.setCode(Constant.successResponse);
			entry.setMessage("找不到对应信息");
		}
		entry.setData(patientVoList);
		return entry;

	}
	
	@ApiOperation(value = "获取业务卡", notes = "入参：hospitalCode：机构id ； patientCode ：患者id ")
	@ResponseBody
	@RequestMapping(value = "/queryBusCards",method = RequestMethod.POST,produces = "application/json")
	public ResponseEntity<List<PatientCardVo>> queryBusCards(@RequestBody PatientCodePojo patientCodePojo) {
		
		ResponseEntity<List<PatientCardVo>> entry = new ResponseEntity();

//		PatientDomain patient = patientService.queryBusCards(  patientCodePojo.getHospitalCode(), patientCodePojo.getPatientCode());
		BaseResponse<List<PatientDomain>> patientListResponse = patientService.queryBusCards(  patientCodePojo.getOrganizationId(), patientCodePojo.getPatientId());
		
		List<PatientCardVo> patientCardVoList = new ArrayList();
		
		 if (patientListResponse.getCode()==Constant.errorTag){
			 entry.setCode(-1);
			 entry.setMessage(patientListResponse.getMessage());
	         return entry;
	     }
		 
		 if(patientListResponse.getCode()!=Constant.successTag){
			 entry.setMessage(patientListResponse.getMessage());
	         return entry;
		 }
		 
		 List<PatientDomain> patientList = patientListResponse.getData();
		//实体bean转换
		if(patientList !=null && patientList.size() > 0){
			
			for(PatientDomain patient : patientList){
				String jzkh;
				String ybkh;

				//改为门诊号码
//				jzkh = patient.getJzkh();
				jzkh = patient.getMzhm();
				
				ybkh = patient.getYbkh();
				
				if(jzkh != null && !"".equals(jzkh)){
					
					PatientCardVo patientCardVo = new PatientCardVo();
					
					patientCardVo.setPatientBirthday(DateUtil.dateFormat(patient.getCsny()));
					patientCardVo.setPatientMobile(patient.getLxdh());
					patientCardVo.setPatientName(patient.getBrxm());
					patientCardVo.setPatientSex(patient.getBrxb());
					patientCardVo.setPatientId(patient.getBrid().toString());
					
					patientCardVo.setPatientMedicalCardType(2);
					patientCardVo.setPatientMedicalCardNumber(jzkh);
					patientCardVoList.add(patientCardVo);
				}
				
				if(ybkh != null && !"".equals(ybkh)){
					PatientCardVo patientCardVo = new PatientCardVo();
					
					patientCardVo.setPatientBirthday(DateUtil.dateFormat(patient.getCsny()));
					patientCardVo.setPatientMobile(patient.getLxdh());
					patientCardVo.setPatientName(patient.getBrxm());
					patientCardVo.setPatientSex(patient.getBrxb());
					patientCardVo.setPatientId(patient.getBrid().toString());
					patientCardVo.setPatientMedicalCardType(3);
					patientCardVo.setPatientMedicalCardNumber(ybkh);
					patientCardVoList.add(patientCardVo);
				}
				/*//同时为空的 返回基本信息
				if((ybkh == null || "".equals(ybkh))&&(jzkh == null || "".equals(jzkh))){
					
					PatientCardVo patientCardVo = new PatientCardVo();
					if(patient.getCsny()!=null){
						patientCardVo.setPatientBirthday(DateUtil.dateFormat(patient.getCsny()));
					}
					
					patientCardVo.setPatientMobile(patient.getLxdh());
					patientCardVo.setPatientName(patient.getBrxm());
					patientCardVo.setPatientSex(patient.getBrxb());
					patientCardVoList.add(patientCardVo);
				}*/
			}
			
		}
		entry.setData(patientCardVoList);
		
		return entry;

	}

	/**
	 * 根据微信绑定的社保卡信息修改病人档案
	 * @param registerParam
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateRegisterInfo",method = RequestMethod.POST,produces = "application/json")
	public ResponseEntity<String> updateRegisterInfo(@RequestBody RegisterParam registerParam){
		ResponseEntity<String> responseEntity = new ResponseEntity<>();
		Integer integer = appointmentService.updateInfo(registerParam.getYbkh(), registerParam.getSfzh(), registerParam.getBrxz());
		if(integer > 0){
			responseEntity.setCode(200);
			responseEntity.setMessage("医保卡号保存成功！");
		} else {
			responseEntity.setCode(-1);
			responseEntity.setMessage("医保卡号保存失败！");
		}
		return responseEntity;
	}

	/**
	 * 微信根据身份证号查询是否绑定医保卡
	 * @param registerParam
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectMedicalCardNum",method = RequestMethod.POST,produces = "application/json")
	public ResponseEntity<String> selectMedicalCardNum(@RequestBody RegisterParam registerParam){
		ResponseEntity<String> responseEntity = new ResponseEntity<>();
		BaseResponse<String> baseResponse = appointmentService.selectMedicalCardNum(registerParam.getSfzh());
		if(baseResponse.getCode() == 200){
			responseEntity.setData(baseResponse.getData());
		}
		responseEntity.setMessage(baseResponse.getMessage());
		responseEntity.setCode(baseResponse.getCode());
		return responseEntity;
	}

}
